<?php

require_once ('../../includes/global.php');

//error_reporting ( E_ALL );


//$fields=Dz("describe ".$_POST['zh']['name']."");


$tables = mysql_list_tables ( $dbname );

echo "<br />
        	#####################################################################
        	<br />
        	#    以下为检查及更新表的结构
        	<br />
        	#####################################################################
        	<br />";
while ( $row = mysql_fetch_row ( $tables ) ) {
	echo "<br />";
    //print "Table: $row[0],\t\t\tupdate web of field had <b>success</b>!\n<br />";
    /*
     * 数据库编码
     */
    Dz ( "ALTER TABLE `{$row[0]}`  DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;" );
    
    /*
     * 必须存在的字段
     */
    $must_had = array (
        
        'id' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `id` INT(10) NOT NULL  FIRST ;
					ALTER TABLE `{$row[0]}` ADD PRIMARY KEY ( `id` ) ; 
					ALTER TABLE `{$row[0]}` CHANGE `id` `id` INT( 10 ) NOT NULL AUTO_INCREMENT;  
					" 
        ), 
        'addtime' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `addtime` INT(10) NOT NULL;
					" 
        ), 
        'sortid' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `sortid` SMALLINT(5) NOT NULL;
					" 
        ), 
        'bdb' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `bdb` VARCHAR(50) NOT NULL;
					" 
        ), 
        'bdid' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `bdid` VARCHAR(50) NOT NULL;
					" 
        ), 
        'lg' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `lg` VARCHAR(20) NOT NULL;
					" 
        ), 
        'del' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `del` TINYINT(1) NOT NULL  DEFAULT '0';
					" 
        ), 
        'gid' => array (
            
            'add' => "
					ALTER TABLE `{$row[0]}` ADD `gid` VARCHAR(50) NOT NULL ;
					" 
        ) 
    );
    $fields = Dz ( "SELECT COLUMN_NAME as Field, COLUMN_COMMENT as comment FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '{$row[0]}' AND table_schema = '{$dbname}'" );
    //Jc($fields);exit;
    

        $old_fields=array();
        foreach ( $fields as $k => $v ) {
            
            $old_fields [] = $v ['Field']; //提取原有的字段
            
        }
        foreach ( $must_had as $must_hadK => $must_hadV ) {
            if (! in_array ( $must_hadK, $old_fields )) {
                $must_hadV ['add'] = explode ( ';', $must_hadV ['add'] );
                foreach ( $must_hadV ['add'] as $sql ) {
                    Dz ( $sql );
                }
                echo "数据表：{$row[0]}添加了新字段:{$must_hadK}<br />";
                ob_flush ();
                flush ();
            } else {
                /*
					 * 这里是修改表单的，看需要吧，如果要自动修改，再在此进行修改就ok了
					 * 不过上面的数组$must_hadV里，要加上与add对就的参数就ok ^_^
					 */
            }
        }
    
}
$dcfg = Dgs ( 'dcfg', "del!=1" );
echo "<br />
        	#####################################################################
        	<br />
        	#    以下为更新表数据的sortid的值
        	<br />
        	#####################################################################
        	<br />";
        

foreach ( $dcfg as $dK => $dV ) {
    $dcfg_json = json_decode ( $dV ['dcfg'] );
    Dz ( "update lm_{$dcfg_json->name} set sortid={$dV['id']} where 1" );
    
    echo "数据表：{lm_{$dcfg_json->name}}将字段sortid的值更新为:{$dV['id']}<br />";
    ob_flush ();
    flush ();
}
echo "<br /><br /><br /><b style='color:green;font-size:15px;'>更新完成</b>";

?>

